package com.keywaysoft.workflow.controller;

import com.keywaysoft.workflow.service.IWorkFlowService;
import com.keywaysoft.workflow.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhaojx
 */
@Api(tags = "流程部署")
@RestController
@RequestMapping("/processDefinition")
@Slf4j
public class ProcessDefinitionController {
    @Autowired
    IWorkFlowService workFlowService;

    @PostMapping(value = "/uploadStreamAndDeployment")
    @ApiOperation(value = "通过bpmn文件部署流程", notes = "通过bpmn文件部署流程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processFile", value = "设计的流程图bpmn文件", dataType = "file", paramType = "__file"),
            @ApiImplicitParam(name = "name", value = "流程部署名称", dataType = "String", paramType = "query")
    })
    public R uploadStreamAndDeployment(@RequestParam("processFile") MultipartFile processFile,
                                       @RequestParam(required = false) String name) {
        return workFlowService.uploadStreamAndDeployment(processFile, name);
    }

    @PostMapping(value = "/addDeploymentByString")
    @ApiOperation(value = "通过bpmn字符串部署流程", notes = "将bpmn文件内容转为字符串然后部署流程，字符串内容即为流程内容")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stringBPMN", value = "bpmn文件内容（字符串）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部署名", dataType = "String", paramType = "query")
    })
    public R addDeploymentByString(@RequestParam("stringBPMN") String stringBPMN,
                                   @RequestParam String name) {
        return workFlowService.addDeploymentByString(stringBPMN, name);
    }

    @GetMapping(value = "/getDeployments")
    @ApiOperation(value = "获取部署列表", notes = "获取部署列表")
    public R getDeployments() {
        return workFlowService.getDeployments();
    }

    @GetMapping(value = "/getDefinitions")
    @ApiOperation(value = "获取流程定义列表", notes = "获取流程定义列表")
    public R getDefinitions() {
        return workFlowService.getDefinitions();
    }

    @GetMapping(value = "/getDefinitionXML")
    @ApiOperation(value = "获取流程定义xml", notes = "获取流程定义xml")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deploymentId", value = "流程部署id", dataType = "String", paramType = "query")
    })
    public void getProcessDefineXML(HttpServletResponse response,
                                    @RequestParam("deploymentId") String deploymentId) {
        workFlowService.getProcessDefineXML(response, deploymentId);
    }


    @GetMapping("/getDefinitionPng")
    @ApiOperation(value = "获取流程定义图片", notes = "获取流程定义图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deploymentId", value = "流程部署id", dataType = "String", paramType = "query")})
    public void getProcessDefinePng(@RequestParam("deploymentId") String deploymentId,
                                    HttpServletResponse response) throws IOException {
        workFlowService.getProcessDefinePng(deploymentId, response);
    }

    /**
     * 挂起/激活流程定义
     *
     * @param key 流程定义key
     * @return
     */
    @PostMapping(value = "suspend/key")
    @ApiOperation(value = "根据流程key挂起/激活流程定义", notes = "根据流程key挂起/激活流程定义")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "流程key", dataType = "String", paramType = "query"),
    })
    public R suspendAllProcessInstance(@RequestParam String key) {
        return workFlowService.suspendAllProcessInstance(key);
    }

    @PostMapping(value = "suspend/id")
    @ApiOperation(value = "根据流程部署ID挂起/激活流程定义", notes = "根据流程部署ID挂起/激活流程定义")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depID", value = "流程部署ID", dataType = "String", paramType = "query"),
    })
    public R suspendAllProcessInstanceByID(@RequestParam String depID) {
        return workFlowService.suspendAllProcessInstanceByID(depID);
    }


    @GetMapping(value = "/del/definition")
    @ApiOperation(value = "删除流程定义", notes = "删除流程定义")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depID", value = "流程部署id", dataType = "String", paramType = "query")
    })
    public R delDefinition(@RequestParam("depID") String depID) {
        return workFlowService.delDefinition(depID);
    }


}
